package com.qdxwx.data.service.category;

import com.qdxwx.common.entity.Ztree;
import com.qdxwx.data.mapper.category.CategoryMapper;
import com.qdxwx.models.category.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类Service业务层处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类
     *
     * @param categoryId 分类ID
     * @return 分类
     */
    @Override
    public Category get(int categoryId) {
        if (categoryId<1) return null;
        return categoryMapper.selectById(categoryId);
    }

    /**
     * 查询分类列表
     *
     * @param category 分类
     * @return 分类
     */
    @Override
    public List<Category> getList(Category category) {
        return categoryMapper.selectList(category);
    }

    @Override
    public List<Category> getRoots(int channelId) {
        if (channelId < 1) return null;
        return categoryMapper.selectRootsByChannelId(channelId);
    }

    @Override
    public List<Category> getRoots(int channelId, int subjectId) {
        if (channelId < 1 || subjectId < 1) return null;
        Category category = new Category();
        category.setChannelId(channelId);
        category.setSubjectId(subjectId);
        category.setParentId(0);
        return categoryMapper.selectList(category);
    }

    @Override
    public List<Category> getChildren(int parentId) {
        if (parentId < 1) return null;
        return categoryMapper.selectChildrenByParentId(parentId);
    }

    @Override
    public int countChildren(int parentId) {
        if (parentId<1) return 0;
        return categoryMapper.countChildren(parentId);
    }

    /**
     * 新增分类
     *
     * @param category 分类
     * @return 结果
     */
    @Override
    public int insert(Category category) {
        category.setCreateTime(LocalDateTime.now());
        int count = categoryMapper.insert(category);
        if (count > 0 && category.getParentId() > 0) {
            Category parent = get(category.getParentId());
            if (parent != null) {
                updateChildCount(parent.getCategoryId(), parent.getChildCount() + 1);
            }
        }
        return count;
    }

    /**
     * 修改分类
     *
     * @param category 分类
     * @return 结果
     */
    @Override
    public int update(Category category) {
        category.setChildCount(countChildren(category.getCategoryId()));
        return categoryMapper.update(category);
    }

    @Override
    public int updateChildCount(int categoryId, int childCount) {
        if (categoryId < 1) return 0;
        if (childCount < 1) categoryId = 0;
        return categoryMapper.updateChildCount(categoryId, childCount);
    }

    /**
     * 删除分类信息
     *
     * @param categoryId 分类ID
     * @return 结果
     */
    @Override
    public int delete(int categoryId) {
        if (categoryId < 1) return 0;
        Category category = get(categoryId);
        if (category == null) return 0;
        int parentId = category.getParentId();
        int count = categoryMapper.deleteById(categoryId);
        if (parentId > 0 && count > 0) {
            updateChildCount(parentId, countChildren(parentId));
        }
        return count;
    }

    @Override
    public List<Integer> getChildIds(int id) {
        List<Integer> arrays = new ArrayList<>();
        for (Category category : getChildren(id)) {
            if (category.getChildCount() > 0) {
                arrays.addAll(getChildIds(category.getCategoryId()));
            } else {
                arrays.add(category.getCategoryId());
            }
        }
        arrays.add(id);
        return arrays;
    }

    @Override
    public List<Category> getParents(int categoryId) {
        return getParents(get(categoryId));
    }

    @Override
    public List<Category> getParents(Category category) {
        List<Category> parents = new ArrayList<>();
        if (category == null) return parents;
        int parentId = category.getParentId();
        while (parentId > 0) {
            Category parent = get(parentId);
            if (parent == null) break;
            parents.add(0, parent);
            parentId = parent.getParentId();
        }
        return parents;
    }

    @Override
    public List<Integer> getParentIds(int id) {
        return getParentIds(get(id));
    }

    @Override
    public List<Integer> getParentIds(Category category) {
        List<Integer> parentIds = new ArrayList<>();
        if (category == null) return parentIds;
        int parentId = category.getParentId();
        while (parentId > 0) {
            Category parent = get(parentId);
            if (parent == null) break;
            parentIds.add(0, parent.getCategoryId());
            parentId = parent.getParentId();
        }
        parentIds.add(category.getCategoryId());
        return parentIds;
    }
    /**
     * 查询分类树列表
     *
     * @return 所有分类信息
     */
    @Override
    public List<Ztree> getTree(int channelId) {
        Category sub = new Category();
        sub.setChannelId(channelId);
        List<Category> categoryList = categoryMapper.selectList(sub);
        List<Ztree> ztrees = new ArrayList<>();
        for (Category category : categoryList) {
            Ztree ztree = new Ztree();
            ztree.setId((long) category.getCategoryId());
            ztree.setpId((long) category.getParentId());
            ztree.setName(category.getCategoryName());
            ztree.setTitle(category.getCategoryName());
            ztree.setSubjectId(category.getSubjectId());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
